/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.appstate;

import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.ui.Picture;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.GongZuoMian_Model;
import model.ZuanChang_Model;
import model.ZuanKong_Model;

/**
 *
 * @author Administrator
 */
public class LittleMapAppState extends AbstractAppState{

    private ZuanKong_Model myzuanKong_Model=new ZuanKong_Model();
    private MyBrain brain;
    private MyController controller;
    private SimpleApplication app;
    private Camera cam;
    private Node rootNode;
    private AssetManager assetManager;
    private Ray ray = new Ray();
    private Node guiNode;
    private Node scaleNode;
    private InputManager inputManager;
    private static Box mesh = new Box(Vector3f.ZERO, 1, 1, 1);
    /////////////////
    private float z_jiaoshun_pos_x=25;
    private float z_jiaoshun_pos_y=85;
    private float z_jiaoshun_len_x=74;
    private float z_jiaoshun_len_y=0;
    private float z_guishun_pos_x=25;
    private float z_guishun_pos_y=37;
    private float z_guishun_len_x=74;
    private float z_guishun_len_y=0;
    private float z_gzm_pos_x=25;
    private float z_gzm_pos_y=85;
    private float z_gzm_len_x=0;
    private float z_gzm_len_y=48;
    //    // 
    private float y_jiaoshun_pos_x=105;
    private float y_jiaoshun_pos_y=85;
    private float y_jiaoshun_len_x=74;
    private float y_jiaoshun_len_y=0;
    private float y_guishun_pos_x=105;
    private float y_guishun_pos_y=37;
    private float y_guishun_len_x=74;
    private float y_guishun_len_y=0;
    private float y_gzm_pos_x=105;
    private float y_gzm_pos_y=85;
    private float y_gzm_len_x=0;
    private float y_gzm_len_y=48;
    private int GZM_TYPE=AllGlobal.GZM_TYPE_LEFT;
    
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app          = (SimpleApplication) app;
        this.cam          = this.app.getCamera();
        this.rootNode     = this.app.getRootNode();
        this.assetManager = this.app.getAssetManager();
        this.guiNode      = this.app.getGuiNode();
        this.inputManager = this.app.getInputManager();
       
        scaleNode=new Node();
        guiNode.attachChild(scaleNode);       
        scaleNode.scale(1.5f);
    }

    @Override
    public void update(float tpf) {
        super.update(tpf); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void cleanup() {
        super.cleanup(); 
        while(scaleNode.getChildren().size()>0){
            scaleNode.getChildren().remove(scaleNode.getChildren().size()-1);
        }
    }
    public void addController(MyController controller){
        this.controller=controller;
    }
    public void setZC(int GZM_TYPE){
        Picture  mapPic=new Picture(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MAP_BACK"));
        mapPic.setImage(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("INTERFACE/LITTLEMAP/GZM_LEFT_LITTLE_MAP.PNG"), false);
        mapPic.move(2, 2, -3);
        mapPic.setWidth(128);
        mapPic.setHeight(128);
        scaleNode.attachChild(mapPic);
        this.GZM_TYPE=GZM_TYPE;
        if(GZM_TYPE==AllGlobal.GZM_TYPE_LEFT){
            try {     
                mapPic.setImage(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("INTERFACE/LITTLEMAP/GZM_LEFT_LITTLE_MAP.PNG"), false);
                ArrayList<GongZuoMian_Model> gzmlist=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
                if(gzmlist.size()>0){
                    GongZuoMian_Model gzmm=gzmlist.get(0);
                    float gzmcd=Float.parseFloat(gzmm.getGzmcd());
                    float gzmzxcd=Float.parseFloat(gzmm.getGzmzxcdString());
                    float shuncao_factor=z_jiaoshun_len_x/gzmzxcd;
                    float gzm_factor=z_gzm_len_y/gzmcd;
                    ArrayList<ZuanChang_Model> zcList= OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANCHANG_SQL);
                    for(int i=0;i<zcList.size();i++){
                        
                        ZuanChang_Model zcm=zcList.get(i);
                        Spatial spatial=scaleNode.getChild(zcm.getMingcheng());
                        if(spatial!=null){
                            spatial.removeFromParent();
                        }
                        float offsetY=Float.parseFloat(zcm.getXdzbx());
                        float offsetX=Float.parseFloat(zcm.getXdzby());

                        Picture zcPic=new Picture(zcm.getMingcheng());
                        zcPic.setImage(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("INTERFACE/LITTLEMAP/ZC.GIF"), false);
                        zcPic.move(z_jiaoshun_pos_x+offsetX*shuncao_factor, z_jiaoshun_pos_y-offsetY*gzm_factor, 4);
                        zcPic.setWidth(4);
                        zcPic.setHeight(4);
                        scaleNode.attachChild(zcPic);
                        
                    }
                }
                
            } catch (Exception ex) {
                Logger.getLogger(LittleMapAppState.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(GZM_TYPE==AllGlobal.GZM_TYPE_RIGHT){
            try {   
                mapPic.setImage(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("INTERFACE/LITTLEMAP/GZM_RIGHT_LITTLE_MAP.PNG"), false);
                ArrayList<GongZuoMian_Model> gzmlist=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
                if(gzmlist.size()>0){
                    GongZuoMian_Model gzmm=gzmlist.get(0);
                    float gzmcd=Float.parseFloat(gzmm.getGzmcd());
                    float gzmzxcd=Float.parseFloat(gzmm.getGzmzxcdString());
                    float shuncao_factor=y_jiaoshun_len_x/gzmzxcd;
                    float gzm_factor=y_gzm_len_y/gzmcd;
                    ArrayList<ZuanChang_Model> zcList= OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANCHANG_SQL);
                    for(int i=0;i<zcList.size();i++){
                        ZuanChang_Model zcm=zcList.get(i);
                        Spatial spatial=scaleNode.getChild(zcm.getMingcheng());
                        if(spatial!=null){
                            spatial.removeFromParent();
                        }
                        float offsetY=Float.parseFloat(zcm.getXdzbx());
                        float offsetX=Float.parseFloat(zcm.getXdzby());

                        Picture zcPic=new Picture(zcm.getMingcheng());
                        zcPic.setImage(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("INTERFACE/LITTLEMAP/ZC.GIF"), false);
                        zcPic.move(y_jiaoshun_pos_x-offsetX*shuncao_factor, y_jiaoshun_pos_y-offsetY*gzm_factor, 4);
                        zcPic.setWidth(4);
                        zcPic.setHeight(4);
                        scaleNode.attachChild(zcPic);

                    }
                }
                
            } catch (Exception ex) {
                Logger.getLogger(LittleMapAppState.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    
}
